package com.example.controller;

import cn.hutool.core.util.ObjectUtil;
import com.example.common.AuthAccess;
import com.example.common.Result;
import com.example.common.RoleE;
import com.example.entity.Account;
import com.example.entity.Admin;
import com.example.entity.Student;
import com.example.service.AdminService;
import com.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class WebController {

    @Resource
    private AdminService adminService;
    @Resource
    private StudentService studentService;
    @AuthAccess
    @PostMapping("/login")
    public Result login(@RequestBody Account account) {
        if(RoleE.ADMIN.name().equals(account.getRole())){
            Account databAdmin=adminService.login(account);
            return Result.success(databAdmin);
        }else if (RoleE.STUDENT.name().equals(account.getRole())){
            Account databStudent=studentService.login(account);
            return Result.success(databStudent);
        }
        else{
            return Result.error("角色错误");
        }
    }
    @AuthAccess
    @PostMapping("/register")
    public Result register(@RequestBody Account account){
        if(ObjectUtil.isEmpty(account.getUsername())||ObjectUtil.isEmpty(account.getPassword())){
            return  Result.error("账号或密码不能为空");
        }
        studentService.register(account);
        return Result.success();
    }
}
